package edu.uc.labs.MobilePrinting.Daemon;

import java.rmi.Naming;

import static edu.uc.labs.MobilePrinting.Configurator.*;

public class MobilePrintListener extends Thread {
	RegistryWrapper rmi = new RegistryWrapper();
	
	public void run() {
		// set the java.rmi.server.codebase property
		// based on the platform
		if(OS.startsWith("Mac"))
			System.setProperty("java.rmi.server.codebase", "file:../libs/MobilePrintingDaemon.jar" );
		//}else{
		//	System.setProperty("java.rmi.server.codebase", "file:" + SYSTEMSPACE + "libs" + DELIM + "MobilePrintingDaemon.jar" );
		//}
		LOGGER.debug("Property for java.rmi.server.codebase = " + System.getProperty("java.rmi.server.codebase"));
		
		// Start the rmiregistry...
		if(!rmi.isAlive())
			rmi.start();
		
		// The registry takes a few seconds to initiate lets wait 5 seconds
		LOGGER.debug("Waiting for the registry to start...");
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e1) { LOGGER.debug("Caught exception while trying to sleep "); }
		
		if(rmi.isAlive()) {
			try{
				Naming.rebind("PrinterProxy", new PrinterProxy());
				LOGGER.debug("The PrinterManager server is ready.");
			} catch (Exception e){
				LOGGER.error("The PrinterManager server failed: " + e.toString());
				return;
			}
		}
	}

	public void halt(){
		rmi.halt();
		return;
	}
	

	public static void main(String[] args) {
		MobilePrintListener m = new MobilePrintListener();
		m.start();
	}
	
	

}
